package com.quanshu.shop.order.view;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.ShopInterface;
import com.quanshu.exchange.support.config.Config;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.LogUtil;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.gui.profile.AllOrderActivity;
import com.quanshu.exchange.ui.gui.profile.order_fragment.DialogDeleteOrder;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.base.view.BaseTitleBar;
import com.quanshu.shop.goods.entity.CartBaseEntity;
import com.quanshu.shop.goods.entity.MyCartEntity;
import com.quanshu.shop.main.api.ShopMainIF;
import com.quanshu.shop.order.SubmitOrderActivity;
import com.quanshu.shop.order.entity.OrderDetailsEntity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OrderDetailsActivity extends BaseActivity {


    @BindView(R.id.order_detail_title)
    BaseTitleBar orderDetailTitle;
    @BindView(R.id.order_detail_bottom)
    ConstraintLayout orderDetailBottom;
    @BindView(R.id.order_detail_icon)
    ImageView orderDetailIcon;
    @BindView(R.id.order_detail_fl_img)
    FrameLayout orderDetailFlImg;
    @BindView(R.id.order_state)
    ConstraintLayout orderState;
    @BindView(R.id.order_address2_img)
    ImageView orderAddress2Img;
    @BindView(R.id.order_address2_br)
    View orderAddress2Br;
    @BindView(R.id.order_address2_txt1)
    TextView orderAddress2Txt1;
    @BindView(R.id.order_address2_txt2)
    TextView orderAddress2Txt2;
    @BindView(R.id.order_address2_txt3)
    TextView orderAddress2Txt3;
    @BindView(R.id.order_detail_address)
    ConstraintLayout orderDetailAddress;
    @BindView(R.id.order_detail_txt1)
    TextView orderDetailTxt1;
    @BindView(R.id.order_detail_line1)
    View orderDetailLine1;
    @BindView(R.id.order_detail_img1)
    ImageView orderDetailImg1;
    @BindView(R.id.order_detail_goods_title)
    TextView orderDetailGoodsTitle;
    @BindView(R.id.order_detail_money1_txt1)
    TextView orderDetailMoney1Txt1;
    @BindView(R.id.order_detail_money1_txt2)
    TextView orderDetailMoney1Txt2;
    @BindView(R.id.order_detail_fl_money)
    FrameLayout orderDetailFlMoney;
    @BindView(R.id.order_detail_txt2)
    TextView orderDetailTxt2;
    @BindView(R.id.order_detail_good_card)
    TextView orderDetailGoodCard;
    @BindView(R.id.order_detail_good_number)
    TextView orderDetailGoodNumber;
    @BindView(R.id.order_detail_cl_goods)
    ConstraintLayout orderDetailClGoods;
    @BindView(R.id.order_detail_info_txt1)
    TextView orderDetailInfoTxt1;
    @BindView(R.id.order_detail_line2)
    View orderDetailLine2;
    @BindView(R.id.order_detail_info_txt)
    TextView orderDetailInfoTxt;
    @BindView(R.id.order_detail_info_txt2)
    TextView orderDetailInfoTxt2;
    @BindView(R.id.order_detail_info_txt3)
    TextView orderDetailInfoTxt3;
    @BindView(R.id.order_detail_info_txt4)
    TextView orderDetailInfoTxt4;
    @BindView(R.id.order_detail_info_txt5)
    TextView orderDetailInfoTxt5;
    @BindView(R.id.order_detail_cl_info)
    ConstraintLayout orderDetailClInfo;
    @BindView(R.id.order_detail_money_txt1)
    TextView orderDetailMoneyTxt1;
    @BindView(R.id.order_detail_line3)
    View orderDetailLine3;
    @BindView(R.id.order_detail_money_txt2)
    TextView orderDetailMoneyTxt2;
    @BindView(R.id.order_detail_money2_txt1)
    TextView orderDetailMoney2Txt1;
    @BindView(R.id.order_detail_money2_txt2)
    TextView orderDetailMoney2Txt2;
    @BindView(R.id.order_detail_fl_money_1)
    FrameLayout orderDetailFlMoney1;
    @BindView(R.id.order_detail_money_txt3)
    TextView orderDetailMoneyTxt3;
    @BindView(R.id.order_detail_money3_txt1)
    TextView orderDetailMoney3Txt1;
    @BindView(R.id.order_detail_money3_txt2)
    TextView orderDetailMoney3Txt2;
    @BindView(R.id.order_detail_fl_money_2)
    FrameLayout orderDetailFlMoney2;
    @BindView(R.id.order_detail_line4)
    View orderDetailLine4;
    @BindView(R.id.order_detail_cl_money)
    ConstraintLayout orderDetailClMoney;
    @BindView(R.id.order_detail_money_sum)
    TextView orderDetailMoneySum;
    @BindView(R.id.order_detail_money4_txt1)
    TextView orderDetailMoney4Txt1;
    @BindView(R.id.order_detail_money4_txt2)
    TextView orderDetailMoney4Txt2;
    @BindView(R.id.order_detail_fl_money_3)
    FrameLayout orderDetailFlMoney3;
    @BindView(R.id.order_detail_cl_money1)
    ConstraintLayout orderDetailClMoney1;
    @BindView(R.id.order_detail_line5)
    View orderDetailLine5;
    @BindView(R.id.bottom_txt1)
    TextView bottomTxt1;
    @BindView(R.id.bottom_txt2)
    TextView bottomTxt2;
    @BindView(R.id.order_detail_bottom1)
    ConstraintLayout orderDetailBottom1;
    @BindView(R.id.order_detail_bottom2)
    ConstraintLayout orderDetailBottom2;
    @BindView(R.id.order_detail_state_txt)
    TextView orderDetailStateTxt;
    @BindView(R.id.bottom_txt3)
    TextView bottomTxt3;
    @BindView(R.id.bottom_txt4)
    TextView bottomTxt4;
    @BindView(R.id.copy_txt)
    TextView copyTxt;
    private String Id;
    private Context mContext;
    private OrderDetailsEntity orderDetailsEntity;
    private DialogDeleteOrder dialogDeleteOrder;
    private CartBaseEntity cartBaseEntity;
    private List<CartBaseEntity> cartBaseEntityList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_details2);
        ButterKnife.bind(this);
        mContext = this;
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initView() {
        ImmersionBar.with(this).titleBar(orderDetailTitle).statusBarColor(R.color.white).statusBarDarkFont(true).init();
        cartBaseEntityList = new ArrayList<>();
        cartBaseEntity = new CartBaseEntity();
        MyCartEntity.GoodsListEntity goodsListEntity = new MyCartEntity.GoodsListEntity();
        cartBaseEntity.setGoodsListEntity(goodsListEntity);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            Id = bundle.getString(AllOrderActivity.ORDER_ID);
            if (!TextUtils.isEmpty(Id)) {
                //获取订单详情
                getOrderDetail(Id);
            }
        }

        orderDetailTitle.setLeftImage(R.drawable.icon_back_black_2_0).setCenterText("订单详情").setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });
        //去结算
        RxViewHelper.getInstance().clicks(bottomTxt1, aVoid -> {
            if (orderDetailsEntity != null) {
                Log.d("结算", "initView: " + orderDetailsEntity.getGid());
                cartBaseEntity.getGoodsListEntity().setGoods_id(orderDetailsEntity.getGid());
                if (!TextUtils.isEmpty(orderDetailsEntity.getGoods_array())) {
                    cartBaseEntity.getGoodsListEntity().setSpec_array(orderDetailsEntity.getGoods_array());
                    cartBaseEntity.getGoodsListEntity().setProduct_id(orderDetailsEntity.getProducts_id());
                } else {
                    cartBaseEntity.getGoodsListEntity().setSpec_array("");
                }
                cartBaseEntity.getGoodsListEntity().setIs_service(orderDetailsEntity.getIs_service() + "");
                cartBaseEntity.getGoodsListEntity().setPayment_options(orderDetailsEntity.getPay_type() + "");
                cartBaseEntity.getGoodsListEntity().setCount(String.valueOf(orderDetailsEntity.getGoods_nums()));
                cartBaseEntityList.add(cartBaseEntity);
                Bundle bundle1 = new Bundle();
                bundle1.putString("order_state", "0");
                bundle1.putString("address_id", orderDetailsEntity.getAddress_id());
                bundle1.putString("order_id", orderDetailsEntity.getOrder_id());
                bundle1.putSerializable("order_data", (Serializable) cartBaseEntityList);
                BaseTool.goActivity(mContext, SubmitOrderActivity.class, bundle1);
            }

        });
        //删除订单
        RxViewHelper.getInstance().clicks(bottomTxt4, aVoid -> {
            doDeleteOrder();
        });
        //取消订单
        RxViewHelper.getInstance().clicks(bottomTxt2, aVoid -> {
            doCancelOrder();
        });
        //完成订单
        RxViewHelper.getInstance().clicks(bottomTxt3, aVoid -> {
            doConfirmOrder();
        });
        //点击复制到粘贴板
        RxViewHelper.getInstance().clicks(copyTxt, aVoid -> {
            try {
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(orderDetailInfoTxt2.getText());
                ToastHelper.toast("复制成功");
            } catch (Exception e) {
                Log.d("复制粘贴板", "initView: " + e);
            }

        });
    }

    //获取订单详情
    private void getOrderDetail(String order_id) {

        NetClient.getInstance().getShopService(ShopMainIF.class)
                .getOrderDetail(AppUtils.getSignParams(order_id), order_id)
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<OrderDetailsEntity>>() {
                    @Override
                    public void onSuccess(BaseResponse<OrderDetailsEntity> response) {
                        orderDetailsEntity = response.getData();
                        orderDetailStateTxt.setText(response.getData().getOrderStatuText());
                        switch (response.getData().getOrderStatu()) {//1：代发货 2 ：待付款 11：待收货  5：已取消  6：已完成 7：已退款 14：退款中
                            case 2:
                                orderDetailIcon.setImageResource(R.drawable.icon_will_pay1_2_0);
                                setBottom(orderDetailBottom);
                                break;
                            case 1:
                                orderDetailIcon.setImageResource(R.drawable.icon_will_transfer1_2_0);
                                setBottom(null);
                                break;
                            case 11:
                                orderDetailIcon.setImageResource(R.drawable.icon_will_getgoods1_2_0);
                                setBottom(orderDetailBottom1);
                                break;
                            case 5:
                                orderDetailIcon.setImageResource(R.drawable.icon_will_cancel1_2_0);
                                setBottom(orderDetailBottom2);
                                break;
                            case 6:
                                orderDetailIcon.setImageResource(R.drawable.icon_will_finish1_2_0);
                                setBottom(orderDetailBottom2);
                                break;
                            case 7:
                                orderDetailIcon.setImageResource(R.drawable.icon_will_finish1_2_0);
                                setBottom(orderDetailBottom2);
                                break;
                        }

                        orderAddress2Txt1.setText("收货人：" + response.getData().getAccept_name());
                        orderAddress2Txt2.setText("联系方式：" + response.getData().getMobile());
                        orderAddress2Txt3.setText("收货地址：" + response.getData().getAddress());
                        Glide.with(mContext).load(Config.URL_HOST_SHOP + response.getData().getImg()).into(orderDetailImg1);
                        orderDetailGoodsTitle.setText(response.getData().getGoods_name());
                        orderDetailMoney1Txt1.setText(response.getData().getSell_price());
                        orderDetailMoney1Txt2.setText(response.getData().getNiu_score());
                        orderDetailMoney2Txt1.setText(response.getData().getSum_price());
                        orderDetailMoney2Txt2.setText(response.getData().getSum_price());
                        orderDetailMoney3Txt1.setText(response.getData().getFreight());
                        orderDetailMoney3Txt2.setText(response.getData().getFreight());
                        BigDecimal bigDecimal = new BigDecimal(response.getData().getSum_price());
                        BigDecimal bigDecimal1 = new BigDecimal(response.getData().getFreight());
                        orderDetailMoney4Txt1.setText(bigDecimal.add(bigDecimal1).toString());
                        orderDetailMoney4Txt2.setText(bigDecimal.add(bigDecimal1).toString());
                        if (response.getData().getPay_type() == 0) {
                            //现金支付
                            orderDetailMoney1Txt1.setVisibility(View.VISIBLE);
                            orderDetailMoney1Txt2.setVisibility(View.GONE);
                            orderDetailMoney2Txt1.setVisibility(View.VISIBLE);
                            orderDetailMoney2Txt2.setVisibility(View.INVISIBLE);
                            orderDetailMoney3Txt1.setVisibility(View.VISIBLE);
                            orderDetailMoney3Txt2.setVisibility(View.INVISIBLE);
                            orderDetailMoney4Txt1.setVisibility(View.VISIBLE);
                            orderDetailMoney4Txt2.setVisibility(View.INVISIBLE);
                        } else {
                            //牛积分支付
                            orderDetailMoney1Txt2.setVisibility(View.VISIBLE);
                            orderDetailMoney1Txt1.setVisibility(View.GONE);
                            orderDetailMoney2Txt1.setVisibility(View.INVISIBLE);
                            orderDetailMoney2Txt2.setVisibility(View.VISIBLE);
                            orderDetailMoney3Txt1.setVisibility(View.INVISIBLE);
                            orderDetailMoney3Txt2.setVisibility(View.VISIBLE);
                            orderDetailMoney4Txt1.setVisibility(View.INVISIBLE);
                            orderDetailMoney4Txt2.setVisibility(View.VISIBLE);
                        }
                        orderDetailGoodCard.setText(response.getData().getGoods_no());
                        orderDetailGoodNumber.setText("X" + response.getData().getGoods_nums());
                        orderDetailInfoTxt2.setText("订  单  号：" + response.getData().getOrder_no());//订单号
                        orderDetailInfoTxt4.setText("创建时间：" + response.getData().getCreate_time());//创建时间
                        orderDetailInfoTxt5.setText("用户留言：" + response.getData().getRemark());//用户留言

                    }

                    @Override
                    public void onFailure(Throwable e) {
                        LogUtil.printException(e);
                    }
                });
    }

    //设置底部显示
    private void setBottom(ConstraintLayout constraintLayout) {
        orderDetailBottom.setVisibility(View.INVISIBLE);
        orderDetailBottom1.setVisibility(View.INVISIBLE);
        orderDetailBottom2.setVisibility(View.INVISIBLE);
        if (constraintLayout != null) {
            constraintLayout.setVisibility(View.VISIBLE);
        }

    }

    //取消订单
    private void doCancelOrder() {

        NetClient.getInstance().getShopService(ShopInterface.class)
                .doCancelOrder(AppUtils.getSignParams(orderDetailsEntity.getOrder_id(), "cancel")
                        , orderDetailsEntity.getOrder_id(), "cancel", orderDetailsEntity.getGoods_type())
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        if (!TextUtils.isEmpty(Id)) {
                            //获取订单详情
                            getOrderDetail(Id);
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        LogUtil.printException(e);
                    }
                });

    }

    //确认收货
    private void doConfirmOrder() {
        NetClient.getInstance().getShopService(ShopInterface.class)
                .doConfirmOrder(AppUtils.getSignParams(orderDetailsEntity.getOrder_id(), "confirm"), orderDetailsEntity.getOrder_id(), "confirm", orderDetailsEntity.getGoods_type())
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        if (!TextUtils.isEmpty(Id)) {
                            //获取订单详情
                            getOrderDetail(Id);
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        LogUtil.printException(e);
                    }
                });
    }

    //删除订单
    private void doDeleteOrder() {
        if (dialogDeleteOrder == null) {
            dialogDeleteOrder = new DialogDeleteOrder(this);
        }
        dialogDeleteOrder.show();
        dialogDeleteOrder.getOkSubject().subscribe(aVoid -> {
            deleteOrder();
        });
    }

    private void deleteOrder() {
        NetClient.getInstance().getShopService(ShopInterface.class)
                .doDeleteOrder(AppUtils.getSignParams(orderDetailsEntity.getOrder_id(), "delete"), orderDetailsEntity.getOrder_id(), "delete", orderDetailsEntity.getGoods_type())
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        finish();
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        LogUtil.printException(e);
                    }
                });
    }
}
